package aiyh.utils.httpUtil;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.Header;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @date 2021/8/31 0031 17:16
 * http请求相应
 */

public class ResponeVo {
	/**
	 * 相应状态码
	 */
	int code;
	/**
	 * 相应内容
	 */
	String entityString;
	/**
	 * 相应头信息
	 */
	@JSONField(serialize = false)
	Header[] allHeaders;
	Locale locale;

	public int getCode() {
		return code;
	}

	/**
	 * 根据相应结果转化为map集合
	 * @return 资源皇后的map集合
	 * @throws JsonProcessingException JSON转换异常
	 */
	public Map<String, Object> getEntityMap() throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.readValue(this.getEntityString(), Map.class);
	}

	/**
	 * 根据相应结果，转化为实体类
	 * @param clazz 需要映射的实体类
	 * @param <T> 需要转换实体类的泛型
	 * @return 转换后的实体类
	 * @throws JsonProcessingException JSON转换异常
	 */
	public <T> T getEntity(Class<T> clazz) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(this.getEntityString(), clazz);
    }

	/**
	 * 根据相应结果转化为实体集合
	 * @param clazz 需要映射的实体类
	 * @param <T> 需要转换的实体类的泛型
	 * @return 转换后的实体类的集合
	 */
	public <T> List<T> getEntityArray(Class<T> clazz) {
		return JSON.parseArray(this.getEntityString(), clazz);
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public Header[] getAllHeaders() {
		return allHeaders;
	}

	public void setAllHeaders(Header[] allHeaders) {
		this.allHeaders = allHeaders;
	}

	public String getEntityString() {
		return entityString;
	}

	public void setEntityString(String entityString) {
		this.entityString = entityString;
	}

	@Override
	public String toString() {
		return "ResponeVo{" +
				"code=" + code +
				", entityString='" + entityString + '\'' +
				", locale=" + locale +
				'}';
	}
}

